package com.fr.data.impl;

import com.fr.base.FRContext;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.dav.Env;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.data.Dictionary;
import com.fr.data.Dictionary.MV;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.data.condition.Condition;
import com.fr.data.core.DataXMLUtils;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;
import com.fr.report.script.core.parser.UtilEvalError;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class FormulaDisplayDictionary
  implements Dictionary
{
  protected Condition condition;
  protected int keyColumnIndex;
  protected int valueColumnIndex;
  protected String formula;

  public int getKeyColumnIndex()
  {
    return this.keyColumnIndex;
  }

  public void setKeyColumnIndex(int paramInt)
  {
    this.keyColumnIndex = paramInt;
  }

  public int getValueColumnIndex()
  {
    return this.valueColumnIndex;
  }

  public void setValueColumnIndex(int paramInt)
  {
    this.valueColumnIndex = paramInt;
  }

  public String getFormula()
  {
    return this.formula;
  }

  public void setFormula(String paramString)
  {
    this.formula = paramString;
  }

  public Condition getCondition()
  {
    return this.condition;
  }

  public void setCondition(Condition paramCondition)
  {
    this.condition = paramCondition;
  }

  public String[] getDependence()
  {
    if (this.condition != null)
      return this.condition.getDependence();
    return new String[0];
  }

  public List createMVList(TableData paramTableData, Calculator paramCalculator)
  {
    Parameter[] arrayOfParameter;
    int j;
    ArrayList localArrayList = new ArrayList();
    TableData localTableData = null;
    HashMap localHashMap = new HashMap();
    if (paramCalculator != null)
    {
      arrayOfParameter = paramTableData.getParameters();
      for (j = 0; j < arrayOfParameter.length; ++j)
      {
        Parameter localParameter = arrayOfParameter[j];
        try
        {
          Object localObject = paramCalculator.eval("$" + localParameter.getName());
          localHashMap.put(localParameter.getName(), localObject);
        }
        catch (UtilEvalError localUtilEvalError)
        {
        }
      }
    }
    try
    {
      localTableData = FRContext.getCurrentEnv().previewTableData(paramTableData, localHashMap, -1);
    }
    catch (Exception localException1)
    {
      FRContext.getLogger().log(Level.WARNING, localException1.getMessage(), localException1);
      localTableData = TableData.EMPTY_TABLEDATA;
    }
    try
    {
      int i = 0;
      j = localTableData.getRowCount();
      while (i < j)
      {
        localArrayList.add(createMV(localTableData, i, paramCalculator));
        ++i;
      }
    }
    catch (TableDataException localTableDataException)
    {
      FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
    }
    try
    {
      localTableData.release();
    }
    catch (Exception localException2)
    {
      FRContext.getLogger().log(Level.WARNING, localException2.getMessage(), localException2);
    }
    return localArrayList;
  }

  protected Dictionary.MV createMV(TableData paramTableData, int paramInt, Calculator paramCalculator)
    throws TableDataException
  {
    Object localObject1 = paramTableData.getValueAt(paramInt, this.keyColumnIndex);
    Object localObject2 = paramTableData.getValueAt(paramInt, this.valueColumnIndex);
    if (getFormula() != null)
    {
      if (paramCalculator == null)
        paramCalculator = Calculator.createCalculator();
      paramCalculator.set("$$$", localObject1);
      try
      {
        localObject2 = paramCalculator.eval(getFormula());
      }
      catch (UtilEvalError localUtilEvalError)
      {
        FRContext.getLogger().log(Level.WARNING, localUtilEvalError.getMessage(), localUtilEvalError);
      }
    }
    return new Dictionary.MV(localObject1, localObject2);
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if ("FormulaDictAttr".equals(str1))
      {
        String str2;
        if ((str2 = paramXMLableReader.getAttr("ki")) != null)
          setKeyColumnIndex(Integer.parseInt(str2));
        if ((str2 = paramXMLableReader.getAttr("vi")) != null)
          setValueColumnIndex(Integer.parseInt(str2));
        if ((str2 = paramXMLableReader.getAttr("formula")) != null)
          setFormula(str2);
        if ((str2 = paramXMLableReader.getAttr("keyColumnIndex")) != null)
          setKeyColumnIndex(Integer.parseInt(str2) - 1);
        if ((str2 = paramXMLableReader.getAttr("valueColumnIndex")) != null)
          setValueColumnIndex(Integer.parseInt(str2) - 1);
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          private final FormulaDisplayDictionary this$0;

          public void readXML()
          {
            if ((paramXMLableReader.isChildNode()) && ("Condition".equals(paramXMLableReader.getTagName())))
              this.this$0.condition = DataXMLUtils.readCondition(paramXMLableReader);
          }
        });
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("FormulaDictAttr").attr("ki", getKeyColumnIndex());
    if (this.formula != null)
      paramXMLPrintWriter.attr("formula", getFormula());
    else
      paramXMLPrintWriter.attr("vi", getValueColumnIndex());
    if (this.condition != null)
      DataXMLUtils.writeXMLCondition(paramXMLPrintWriter, this.condition);
    paramXMLPrintWriter.end();
  }

  public boolean equals(Object paramObject)
  {
    if (paramObject == this)
      return true;
    if (!(paramObject instanceof FormulaDisplayDictionary))
      return false;
    return ((getKeyColumnIndex() == ((FormulaDisplayDictionary)paramObject).getKeyColumnIndex()) && (getValueColumnIndex() == ((FormulaDisplayDictionary)paramObject).getValueColumnIndex()) && (ComparatorUtils.equals(this.formula, ((FormulaDisplayDictionary)paramObject).getFormula())));
  }

  public String toString()
  {
    return super.getClass().getName();
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    FormulaDisplayDictionary localFormulaDisplayDictionary = (FormulaDisplayDictionary)super.clone();
    if (this.condition != null)
      localFormulaDisplayDictionary.condition = ((Condition)this.condition.clone());
    return localFormulaDisplayDictionary;
  }
}